package com.yocard.yxyd.ui.view;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yocard.yxyd.others.EditTextWatcher;
import com.yocard.yxyd.utils.util.KeyboardUtils;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.yocard.yxyd.R;
import com.yocard.yxyd.http.model.request.CsmListRequest;
import com.yocard.yxyd.others.DecimalDigitsInputFilter;
import com.yocard.yxyd.utils.BigDecimalUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created on 2019/4/10.
 */
public class CsmDrawerView extends FrameLayout {

    @BindView(R.id.flow_jilu)
    TagFlowLayout flowJilu;
    //    @BindView(R.id.flow_tuidan)
//    TagFlowLayout flowTuidan;
//    @BindView(R.id.flow_jine)
//    TagFlowLayout flowJine;
    //    @BindView(R.id.flow_type)
//    TagFlowLayout flowType;
    @BindView(R.id.flow_shijian)
    TagFlowLayout flowSj;
    @BindView(R.id.flow_ziliao)
    TagFlowLayout flowZiliao;
    @BindView(R.id.et_start_amount)
    AppCompatEditText etStartAmount;
    @BindView(R.id.et_end_amount)
    AppCompatEditText etEndAmount;

    private ValueCallback<CsmListRequest> callback;

    protected CsmListRequest request;
    protected Resources mResources;
    String jilu;
    protected String shijianField;

    public CsmDrawerView(@NonNull Context context) {
        this(context, null);
    }

    public CsmDrawerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CsmDrawerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @OnClick({R.id.tv_reset, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                ok();
                break;
            case R.id.tv_reset:
                resetValues();
                if (callback != null) {
                    callback.onReceiveValue(null);
                }
                break;
        }
    }

    public void resetValues() {
        request = new CsmListRequest();
        etStartAmount.setText("");
        etEndAmount.setText("");
        jilu = "";

        flowJilu.onChanged();
//        flowJine.onChanged();
        flowSj.onChanged();
        flowZiliao.onChanged();
    }

    private void ok() {
        String startAmount = etStartAmount.getText().toString().trim();
        String endAmount = etEndAmount.getText().toString().trim();
        if (startAmount.equals(".") || endAmount.equals(".")) {
            ToastUtils.showShort("请正确输入贷款金额");
            return;
        }
        if (BigDecimalUtil.compareTo(startAmount, endAmount) == 1) {
            ToastUtils.showShort("请正确输入贷款金额范围");
            return;
        }

        if (BigDecimalUtil.compareTo(startAmount, "0.5") == -1
                || BigDecimalUtil.compareTo(endAmount, "0.5") == -1) {
            ToastUtils.showShort("最低金额不能低于0.5万");
            return;
        }

        request.setStart_amount(startAmount);
        request.setEnd_amount(endAmount);
        request.setHandle_status(jilu);
        request.setBuy_time(shijianField);

        if (callback != null) {
            callback.onReceiveValue(request);
        }

//        resetValues();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.drawer_custom, this);
        ButterKnife.bind(this);
        request = new CsmListRequest();
        mResources = getResources();
        etStartAmount.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(1)});
        etEndAmount.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(1)});
        etStartAmount.addTextChangedListener(new EditTextWatcher(etStartAmount));
        etEndAmount.addTextChangedListener(new EditTextWatcher(etEndAmount));
        initJilu();
//        initTuidan();
//        initJine();
//        initType();
        initShijian();
        initZiliao();
    }

    private void initZiliao() {
        flowZiliao.setAdapter(new TagAdapter<String>(getResources().getStringArray(R.array.ziliao)) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowZiliao, false);
                tv.setText(s);
                return tv;
            }
        });
        flowZiliao.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                KeyboardUtils.hideSoftInput(CsmDrawerView.this);

                TagView tagView = (TagView) view;
                if(tagView.isChecked()){
                    switch (position) {
                        case 0:
                            request.setIs_social_insurance(1);
                            break;
                        case 1:
                            request.setIs_fund(1);
                            break;
                        case 2:
                            request.setIs_car(1);
                            break;
                        case 3:
                            request.setIs_house(1);
                            break;
                        case 4:
                            request.setIs_zhima(1);
                            break;
                        case 5:
                            request.setIs_weili(1);
                            break;
                        case 6:
                            request.setIs_bxbd(1);
                            break;
                    }
                }else {
                    switch (position) {
                        case 0:
                            request.setIs_social_insurance(0);
                            break;
                        case 1:
                            request.setIs_fund(0);
                            break;
                        case 2:
                            request.setIs_car(0);
                            break;
                        case 3:
                            request.setIs_house(0);
                            break;
                        case 4:
                            request.setIs_zhima(0);
                            break;
                        case 5:
                            request.setIs_weili(0);
                            break;
                        case 6:
                            request.setIs_bxbd(0);
                            break;
                    }
                }
                return false;
            }
        });
    }

    private void initShijian() {
        String[] sjField = mResources.getStringArray(R.array.csm_shijian_fied);
        flowSj.setAdapter(new TagAdapter<String>(getResources().getStringArray(R.array.csm_shijian)) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowSj, false);
                tv.setText(s);
                return tv;
            }
        });
        flowSj.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                TagView tv = (TagView) view;
                if (tv.isChecked()) {
                    shijianField = sjField[position];
                } else {
                    shijianField = "";
                }
                KeyboardUtils.hideSoftInput(CsmDrawerView.this);
                return false;
            }
        });
    }

//    private void initType() {
//        flowType.setAdapter(new TagAdapter<String>(getResources().getStringArray(R.array.csm_type)) {
//            @Override
//            public View getView(FlowLayout parent, int position, String s) {
//                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowType, false);
//                tv.setText(s);
//                return tv;
//            }
//        });
//    }

//    private void initJine() {
//        flowJine.setAdapter(new TagAdapter<String>(getResources().getStringArray(R.array.csm_jine)) {
//            @Override
//            public View getView(FlowLayout parent, int position, String s) {
//                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowJine, false);
//                tv.setText(s);
//                return tv;
//            }
//        });
//    }

//    private void initTuidan() {
//        flowTuidan.setAdapter(new TagAdapter<String>(getResources().getStringArray(R.array.csm_tuidan)) {
//            @Override
//            public View getView(FlowLayout parent, int position, String s) {
//                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowTuidan, false);
//                tv.setText(s);
//                return tv;
//            }
//        });
//    }


    private void initJilu() {
        String[] jiluFied = mResources.getStringArray(R.array.csm_jilu_fied);
        flowJilu.setAdapter(new TagAdapter<String>(mResources.getStringArray(R.array.csm_jilu)) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowJilu, false);
                tv.setText(s);
                return tv;
            }
        });
        flowJilu.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                TagView tv = (TagView) view;
                if (tv.isChecked()) {
                    jilu = jiluFied[position];
                } else {
                    jilu = "";
                }
                KeyboardUtils.hideSoftInput(CsmDrawerView.this);
                return false;
            }
        });
    }

    public void setSelectListener(ValueCallback<CsmListRequest> callback) {
        this.callback = callback;
    }
}
